<?php
/**
 * Created by PhpStorm.
 * User: xuho
 * Date: 2020-09-02
 * Time: 17:10
 */

namespace extend\payment;


class FuYouPreService extends FuYouBaseService
{
    public $domain = '';

    // WXPAJS-微信预授权，ALPAJS-支付宝预授权
    public $tradeType = ['1' => 'WXPAJS', '2' => 'ALPAJS'];
    // WXPA-微信预授权，ALPA-支付宝预授权
    public $orderType = ['1' => 'WXPA', '2' => 'ALPA'];


    /**
     * 条码支付，商户扫客户
     * @param $request
     * @return mixed
     * @throws \Exception
     */
    public function microOrder($request)
    {
        // 接口类型
        $apiType = 'order_create';

        // 获取支付参数
        $paramSign = $this->beforeRequest($request, $apiType);
        // 接口地址
        $urlApi = '/micropay';

        $paramSign['order_type'] = $this->orderType[$request['order_type']];
        $paramSign['goods_des'] = $request['goods_des'];
        $paramSign['order_amt'] = $request['order_amt'];
        $paramSign['mchnt_order_no'] = $request['mchnt_order_no'];
        // 授权码
        $paramSign['auth_code'] = $request['auth_code'];

        // 非必填数据
        $paramSign['goods_detail'] = $request['goods_detail'] ?? '';
        $paramSign['goods_tag'] = $request['goods_tag'] ?? '';

        // 支付场景：1-条形支付 2-声波支付
        $paramSign['sence'] = $request['sence'] ?? '1';

        // 不参与校验的数据
        $paramAttach['reserved_sub_appid'] = $request['reserved_sub_appid'] ?? '';
        $paramAttach['reserved_limit_pay'] = $request['reserved_limit_pay'] ?? '';
        $paramAttach['reserved_expire_minute'] = $request['reserved_expire_minute'] ?? '';
        $paramAttach['reserved_fy_term_id'] = $request['reserved_fy_term_id'] ?? '';
        $paramAttach['reserved_fy_term_type'] = $request['reserved_fy_term_type'] ?? '';
        $paramAttach['reserved_fy_term_sn'] = $request['reserved_fy_term_sn'] ?? '';

        $response = $this->sendRequest($urlApi, $paramSign, $paramAttach);

        return $this->afterRequest($response, $apiType);

    }

    /**
     * 扫码支付，客户扫商户，主扫
     * @param array $request
     * @return mixed
     * @throws \Exception
     */
    public function codeOrder($request = [])
    {
        // 接口类型
        $apiType = 'order_create';

        // 获取支付参数
        $paramSign = $this->beforeRequest($request, $apiType);
        // 接口地址
        $urlApi = '/preCreate';

        // 必填数据
        $paramSign['order_type'] = $this->orderType[$request['order_type']];
        $paramSign['goods_des'] = $request['goods_des'];
        $paramSign['order_amt'] = $request['order_amt'];
        $paramSign['notify_url'] = $request['notify_url'];
        $paramSign['mchnt_order_no'] = $request['mchnt_order_no'];

        // 非必填数据
        $paramSign['goods_detail'] = $request['goods_detail'] ?? '';
        $paramSign['goods_tag'] = $request['goods_tag'] ?? '';

        // 不参与校验的数据
        $paramAttach['reserved_sub_appid'] = $request['reserved_sub_appid'] ?? '';
        $paramAttach['reserved_limit_pay'] = $request['reserved_limit_pay'] ?? '';
        $paramAttach['reserved_expire_minute'] = $request['reserved_expire_minute'] ?? '';
        $paramAttach['reserved_fy_term_id'] = $request['reserved_fy_term_id'] ?? '';
        $paramAttach['reserved_fy_term_type'] = $request['reserved_fy_term_type'] ?? '';
        $paramAttach['reserved_fy_term_sn'] = $request['reserved_fy_term_sn'] ?? '';

        $response = $this->sendRequest($urlApi, $paramSign, $paramAttach);

        return $this->afterRequest($response, $apiType);
    }

    /**
     * 统一下订单，服务窗模式
     * 自定义生成订单方式
     * @param $request
     * @return mixed
     * @throws \Exception
     */
    public function unifiedOrder($request)
    {
        // 接口类型
        $apiType = 'order_create';

        // 获取支付参数
        $paramSign = $this->beforeRequest($request, $apiType);
        // 接口地址
        $urlApi = '/wxPreCreate';

        // 必填数据
        $paramSign['goods_des'] = $request['goods_des'];
        $paramSign['order_amt'] = $request['order_amt'];
        $paramSign['notify_url'] = $request['notify_url'];
        $paramSign['mchnt_order_no'] = $request['mchnt_order_no'];
        $paramSign['trade_type'] = $this->tradeType[$request['trade_type']];


        // 非必填数据
        $paramSign['openid'] = $request['openid'] ?? '';
        $paramSign['sub_openid'] = $request['sub_openid'] ?? '';
        $paramSign['sub_appid'] = $request['sub_appid'] ?? '';

        $paramSign['goods_detail'] = $request['goods_detail'] ?? '';
        $paramSign['goods_tag'] = $request['goods_tag'] ?? '';
        $paramSign['product_id'] = $request['product_id'] ?? '';
        $paramSign['limit_pay'] = $request['limit_pay'] ?? '';


        // 不参与校验的数据
        $paramAttach['reserved_expire_minute'] = $request['reserved_expire_minute'] ?? '';
        $paramAttach['reserved_fy_term_id'] = $request['reserved_fy_term_id'] ?? '';
        $paramAttach['reserved_fy_term_type'] = $request['reserved_fy_term_type'] ?? '';
        $paramAttach['reserved_txn_bonus'] = $request['Reserved_txn_bonus'] ?? '';
        $paramAttach['reserved_fy_term_sn'] = $request['reserved_fy_term_sn'] ?? '';


        $response = $this->sendRequest($urlApi, $paramSign, $paramAttach);

        return $this->afterRequest($response, $apiType);
    }

    /**
     * 订单查询
     * @param $request
     * @return mixed
     * @throws \Exception
     */
    public function orderQuery($request)
    {
        // 接口类型
        $apiType = 'order';

        // 获取支付参数
        $paramSign = $this->beforeRequest($request, $apiType);
        // 接口地址
        $urlApi = '/commonQuery';

        // 必填数据
        $paramSign['mchnt_order_no'] = $request['mchnt_order_no'];
        $paramSign['order_type'] = $this->orderType[$request['order_type']];

        $response = $this->sendRequest($urlApi, $paramSign);

        return $this->afterRequest($response, $apiType);
    }

    /**
     * 预授权完成撤销
     * @param $request
     * @return mixed
     * @throws \Exception
     */
    public function preComplete($request)
    {
        // 接口类型
        $apiType = 'pre_order';
        // 获取支付参数
        $paramSign = $this->beforeRequest($request, $apiType);
        // 接口地址
        $urlApi = '/preAuthFinish';

        // 必填数据
        $paramSign['orig_order_no'] = $request['orig_order_no'];
        $paramSign['mchnt_order_no'] = $request['mchnt_order_no'];
        $paramSign['order_type'] = $this->orderType[$request['order_type']];
        $paramSign['order_amt'] = $request['order_amt'];
        $paramSign['finish_amt'] = $request['finish_amt'];

        // 不参与校验的数据
        $paramAttach['reserved_teller_info'] = $param['reserved_teller_info'] ?? '';
        $paramAttach['reserved_fy_term_id'] = $param['reserved_fy_term_id'] ?? '';

        $response = $this->sendRequest($urlApi, $paramSign, $paramAttach);

        return $this->afterRequest($response, $apiType);
    }

    /**
     * 预授权撤销
     * @param $request
     * @return mixed
     * @throws \Exception
     */
    public function preCancel($request)
    {
        // 接口类型
        $apiType = 'order';
        // 获取支付参数
        $paramSign = $this->beforeRequest($request, $apiType);
        // 接口地址
        $urlApi = '/preAuthCancel';

        // 必填数据
        $paramSign['orig_order_no'] = $request['orig_order_no'];
        $paramSign['mchnt_order_no'] = $request['mchnt_order_no'];
        $paramSign['order_type'] = $this->orderType[$request['order_type']];
        $paramSign['order_amt'] = $request['order_amt'];

        // 不参与校验的数据
        $paramAttach['reserved_teller_info'] = $param['reserved_teller_info'] ?? '';
        $paramAttach['reserved_fy_term_id'] = $param['reserved_fy_term_id'] ?? '';

        $response = $this->sendRequest($urlApi, $paramSign, $paramAttach);

        return $this->afterRequest($response, $apiType);
    }

    /**
     * 预授权查询
     * @param $request
     * @return mixed
     * @throws \Exception
     */
    public function preQuery($request)
    {
        // 接口类型
        $apiType = 'order';
        // 获取支付参数
        $paramSign = $this->beforeRequest($request, $apiType);
        // 接口地址
        $urlApi = '/preAuthQuery';

        // 必填数据
        $paramSign['mchnt_order_no'] = $request['mchnt_order_no'];
        $paramSign['order_type'] = $this->orderType[$request['order_type']];

        $response = $this->sendRequest($urlApi, $paramSign);

        return $this->afterRequest($response, $apiType);
    }

    /**
     * @param $request
     * @return mixed
     * @throws \Exception
     */
    public function preCompleteCancel($request)
    {
        // 接口类型
        $apiType = 'order';
        // 获取支付参数
        $paramSign = $this->beforeRequest($request, $apiType);
        // 接口地址
        $urlApi = '/preAuthFinishCancel';

        // 必填数据
        $paramSign['orig_order_no'] = $request['orig_order_no'];
        $paramSign['mchnt_order_no'] = $request['mchnt_order_no'];
        $paramSign['order_type'] = $this->orderType[$request['order_type']];

        // 不参与校验的数据
        $paramAttach['reserved_teller_info'] = $param['reserved_teller_info'] ?? '';
        $paramAttach['reserved_fy_term_id'] = $param['reserved_fy_term_id'] ?? '';

        $response = $this->sendRequest($urlApi, $paramSign, $paramAttach);

        return $this->afterRequest($response, $apiType);
    }

    /**
     * 请求前操作
     * @param array $request
     * @param string $type
     * @return mixed
     */
    public function beforeRequest($request = [], $type = 'order')
    {
        $this->domain = $request['domain'];
        $this->privateKey = $request['private_key'];

        // 机构号
        $paramSign['ins_cd'] = $request['ins_cd'];
        // 商户号
        $paramSign['mchnt_cd'] = $request['mchnt_cd'];
        // 随机字符串
        $paramSign['random_str'] = nonceStr();
        // 版本
        $paramSign['version'] = $request['version'];
        // 终端号(没有真实终端号统一填88888888)
        $paramSign['term_id'] = $request['term_id'];

        // 判断是否为创建订单接口
        if ($type == 'order_create') {
            // 附加数据
            $paramSign['addn_inf'] = '';
            // 交易时间
            $paramSign['txn_begin_ts'] = date('YmdHis', time());
            // 货币类型
            $paramSign['curr_type'] = $request['curr_type'];
            // ip 地址
            $paramSign['term_ip'] = $request['term_ip'];
        }

        return $paramSign;
    }

    /**
     * 返回结果
     * @param $response
     * @param string $response_type
     *  共4种类型 ： order_create、order、withdraw、settle
     * @return mixed
     * @throws \Exception
     */
    public function afterRequest($response, $response_type)
    {
        $response = xml2array($response);
        var_dump($response);
        if (strpos($response_type, 'order') !== false) {
            // 订单类型
            if ($response['result_code'] == '000000') {
                $result['status'] = '1';
                $result['msg'] = 'success';
                $result['data'] = $response;
            } else {
                $result['status'] = '0';
                $result['msg'] = $response['result_msg'];
            }
        } else if ($response_type == 'withdraw') {
            // 提现类型
            if ($response['result_code'] == 'SUCCESS') {
                $result['status'] = '1';
                $result['msg'] = 'success';
                $result['data'] = $response;
            } else {
                $result['status'] = '0';
                $result['msg'] = $response['return_msg'];
            }
        } else {
            $result['status'] = '1';
            $result['msg'] = 'success';
            $result['data'] = $response;
        }
        return $result;
    }
}